package com.szxy.miaosha.controller;

import com.szxy.miaosha.domain.Users;
import com.szxy.miaosha.redis.RedisService;
import com.szxy.miaosha.redis.UserPrefixKey;
import com.szxy.miaosha.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @Auther:wzer
 * @Date:2020/9/5
 * @Description:com.szxy.miaosha.controller
 * @Version:1.0
 **/
@Controller
@RequestMapping("/demo")
public class SimpleController {

    @Autowired
    private RedisService redisService;


    @RequestMapping("/test")
    public String test(Model model) {
        model.addAttribute("name", "miaosha 秒杀");
        return "test";
    }

    @RequestMapping("/get/{key}")
    @ResponseBody
    public Result get(@PathVariable String key) {
        String data = redisService.get(key, String.class);
        return Result.success(data);
    }

    @RequestMapping("/set/{key}/{value}")
    @ResponseBody
    public Result set(@PathVariable String key, @PathVariable String value) {
        boolean flag = redisService.set(key, value);
        return Result.success(flag);
    }

    @RequestMapping("/setUser")
    @ResponseBody
    public Result set() {
        boolean flag = redisService.set(UserPrefixKey.getById, "1", new Users(1, "Hello?Word"));
        return Result.success(flag);
    }

    @RequestMapping("/getUser")
    @ResponseBody
    public Result getUser() {
        Users user = redisService.get(UserPrefixKey.getById, "1", Users.class);
        return Result.success(user);
    }


}
